# _*_ coding:utf-8 _*_
# @Time  : 2022.09.19
# @Author: zizlee
import datetime
import json
import pandas as pd
import requests
import time
from urllib3 import disable_warnings
disable_warnings()


HOST = 'https://210.13.218.130:9000/'
# HOST = 'http://127.0.0.1:8000/'


# 进度打印
def print_progress_bar(current, total, color=35, title=''):  # percent是小数
    percent = round(current/total, 4)
    prefix_progress = '▓' * int(80 * percent)
    leave_progress = '▓' * (80 - int(80 * percent))
    # percent = '%.2f' % (percent * 100)
    print(f'\033[1;{color}m{current}/{total}\033[0m|\033[1;{color}m{prefix_progress}\033[0m{leave_progress} {title}')


def get_spot_price(start, end, variety):
    url = HOST + 'v1/datan/spot/price/'
    r = requests.get(url, params={'start': start, 'end': end, 'v': variety}, verify=False)
    df = pd.DataFrame(r.json()['data'])
    if df.empty:
        return []
    df = df[['spot_date', 'price']]
    df.rename(columns={'spot_date': 'datadate', 'price': 'datavalue'}, inplace=True)
    df.sort_values(by='datadate', inplace=True)
    return df.to_dict(orient='records')


def get_indexes():  # 获取待更新的指标
    with open('R_现货价格.json', encoding='utf8', mode='r') as fp:
        indexes = json.load(fp)
    print('读取现货指标数量:', len(indexes))
    # 读取网络指标数据，过滤并修改起始日期
    r = requests.get(HOST + 'v1/datalib/table/indexGroups/', verify=False)
    customer_indexes = []
    for g in r.json()['data']:
        if g['group'] == 'rj1':
            customer_indexes = g['data']
    local_indexes = {i['source_id']: i['variety_en'] for i in indexes}
    customer_indexes = list(filter(lambda x: x['source_id'] in local_indexes.keys(), customer_indexes))
    for ci in customer_indexes:
        ci['variety_en'] = local_indexes.get(ci['source_id'])
        if not ci['variety_en']:
            raise ValueError(f'{ci["name_zh"]}品种不存在！')
    print('待更新现货指标数量:', len(customer_indexes))
    return customer_indexes


def handle_indexes(index_list):
    total_count = len(index_list)
    today = datetime.datetime.today().strftime('%Y%m%d')
    for index, row in enumerate(index_list):
        time.sleep(1)
        start_date = row['enddate'] if row['enddate'] else row['startdate']
        spot_price_list = get_spot_price(
            start=start_date.replace('-', ''),
            end=today,
            variety=row['variety_en']
        )
        if len(spot_price_list) < 1:
            print('没有获取到{}的现货数据!'.format(row['name_zh']))
            continue
        if len(spot_price_list) == 1:
            msg = f'{row["name_zh"]},起始日期:{start_date}已存在,略...'
            print_progress_bar(current=index + 1, total=total_count, color=36, title=msg)
            continue
        print(spot_price_list)
        save_data_to_server(sheet_id=row['id'], datalist=spot_price_list)
        msg = f'{row["name_zh"]}指标{row["source_id"]},起始日期:{start_date}保存完成.'
        print_progress_bar(current=index + 1, total=total_count, color=35, title=msg)


def get_keyword_indexes(kw):
    url = 'https://210.13.218.130:9000/v1/datalib/table/'
    prams = {
        'kw': kw,
        'page': 1,
        'page_size': 100
    }
    r = requests.get(url, params=prams, verify=False)
    data = r.json()['data']['records']
    return data


def get_rdb_data(formula):
    r = requests.post('https://210.13.218.130:9000/v1/datalib/indexAis/formulaData/', verify=False,
                      json={'formula': formula})
    resp_data = r.json()['data']
    return resp_data['data']


def update_basic():  # 更新基差数据
    basic_data = get_keyword_indexes(kw='基差:')
    with open('R_基差.json', 'r', encoding='utf8') as f:
        configs = json.load(f)
    for c in configs:
        time.sleep(0.8)
        for b in basic_data:
            if c['id'] == b['id'] and b['enddate']:
                c['enddate'] = b['enddate']
                break
        data_list = get_rdb_data(formula=c['formula'])
        start = c['enddate'] if c['enddate'] else c['startdate']
        data_list = list(filter(lambda x: x['datadate'] >= start, data_list))
        # 找出没有的数据项
        no_data_list = list(filter(lambda x: str(x['datavalue']) == '' and datetime.datetime.strptime(x['datadate'], '%Y-%m-%d').weekday() <= 4, data_list))
        for n in no_data_list:
            print(f'{c["name_zh"]}没有{n["datadate"]}基差!')
        if len(no_data_list) > 0:
            continue
        if len(data_list) <= 1:
            print(f'{c["name_zh"]} {start}起始的基差没有新数据!')
            continue
        data_list = list(filter(lambda x: str(x['datavalue']) != '', data_list))
        save_data_to_server(c['id'], data_list)
        print(f'{c["name_zh"]} {start}起始的基差更新完成!')


# 保存数据到指标中
def save_data_to_server(sheet_id, datalist):
    url = 'https://210.13.218.130:9000/v1/datalib/table/update/'
    body_data = {
        'dataid': sheet_id,
        'row_value': datalist
    }
    try:
        r = requests.post(url, json=body_data, verify=False)
        if r.json()['code'] != 200:
            raise ValueError(f'status_code={r.json()["code"]}')
    except Exception as e:
        if datalist:
            msg = f'保存数据指标 ID={sheet_id}失败了：{e}'
        else:
            msg = f'更新指标 ID={sheet_id} 刷新日期失败了：{e}'
        print(msg)
    else:
        pass


if __name__ == '__main__':
    spot_indexes = get_indexes()
    # for i in spot_indexes:
    #     print(i)
    # 过滤掉今日已更新的数据
    spot_indexes = list(filter(lambda x: x['enddate'] < datetime.datetime.today().strftime('%Y-%m-%d') if x['enddate'] else True, spot_indexes))
    # spot_indexes = list(filter(lambda x: x['enddate'] < '2023-09-18' if x['enddate'] else True, spot_indexes))
    # spot_indexes = list(filter(lambda x: x['variety_en'] in ['A'], spot_indexes))
    handle_indexes(spot_indexes)
    update_basic()  # 更新基差数据
